package com.aier.controller.purchase;

import com.aier.bean.common.CommonResult;
import com.aier.bean.common.Status;
import com.aier.bean.purchase.PurchaseBean;
import com.aier.service.purchase.PurchaseService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Map;

/**
 * 对Purchase表的一些操作
 */

@Controller
public class PurchaseController {

    @Resource
    PurchaseService purchaseService;

    @RequestMapping("/enter_purchase")
    @ResponseBody
    public CommonResult enterProduct(@RequestBody PurchaseBean purchaseBean){
        if (purchaseBean.getOrderDate() == null){
            purchaseBean.setOrderDate(LocalDate.now());
        }
        purchaseService.contractCodeIsExist(purchaseBean);
        purchaseBean.setStatus(Status.DOING);
        return purchaseService.savePurchase(purchaseBean);
    }

    @RequestMapping("/purchase_all_contract_code")
    @ResponseBody
    public CommonResult getAllOrderId(){
        return purchaseService.getAllContractCode();
    }


    @RequestMapping("/purchase_get_model_name_by_contract_code")
    @ResponseBody
    public CommonResult getAllModelName(@RequestBody Map<String,String> map){
        String contractCode = map.get("contractCode");
        return purchaseService.getModeNameByContractCode(contractCode);
    }

}
